package pos.server.dac.dao;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.sql.*;
import pos.domain.Utente;
import pos.client.dac.dao.UtenteDAO;
import pos.server.dac.ConnectionManager;
import pos.server.dac.exceptions.EccezioneCRUD;

/**
 *
 * @author Alessio
 */
public class UtenteDAOImpl extends RemoteServiceServlet implements UtenteDAO
{
    private String SELECT_SQL = "SELECT * FROM utente WHERE login = ?";
    private String INSERT_SQL = "INSERT INTO utente VALUES (?,?,?)";
    private String UPDATE_SQL = "UPDATE utente SET password = ?, ultima_modifica = ? WHERE login = ?";
    private String DELETE_SQL = "DELETE FROM utente WHERE login = ?";
    
    @Override
    public Utente leggiDatidaDB(Utente obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(SELECT_SQL);
            pstmt.setString(1, obj.getLogin());
            ResultSet result = pstmt.executeQuery();
            if(result.next()){
                obj.setPassword(result.getString("password"));
                obj.setUltimaModifica(result.getTimestamp("ultima_modifica").getTime());
            }
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
        return obj;
    }

    @Override
    public void inserisciDatisuDB(Utente obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_SQL);
            pstmt.setString(1, obj.getLogin());
            pstmt.setString(2, obj.getPassword());
            pstmt.setTimestamp(3, new Timestamp(obj.getUltimaModifica()));
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void aggiornaDatisuDB(Utente obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_SQL);
            pstmt.setString(3, obj.getLogin());
            pstmt.setString(1, obj.getPassword());
            pstmt.setTimestamp(2, new Timestamp(obj.getUltimaModifica()));
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void cancellaDatidaDB(Utente obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_SQL);
            pstmt.setString(1, obj.getLogin());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }
}
